//
//  AESEncyptUtil.swift
//  SBSafetyApp
//
//  Created by Lina on 2024/7/2.
//

import Foundation
import CryptoSwift

/// 加密工具类
class AESEncyptUtil: NSObject {
    /// AES_CBC 加密
    static func encryptCBC(encryptText: String, key: String, ivs: String) throws -> String {
//        let iv = AES.randomIV(AES.blockSize)
//        let aes = try AES(key: Padding.zeroPadding.add(to: key.bytes, blockSize: AES.blockSize), blockMode: CBC(iv: ivs.bytes))
        let aes = try AES(key: key.bytes, blockMode: CBC(iv: ivs.bytes), padding: .zeroPadding)
        let encrypted = try aes.encrypt(encryptText.bytes)
        return encrypted.toBase64()
    }

    /// AES_CBC 解密
    static func decryptCBC(decryptText: String, key: String, ivs: String) throws -> String {
        let aes = try AES(key: Padding.zeroPadding.add(to: key.bytes, blockSize: AES.blockSize), blockMode: CBC(iv: ivs.bytes))
        let decrypted = try decryptText.decryptBase64ToString(cipher: aes)
        return decrypted
    }
}
